package mspbots.cw.business.core.slas;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.cw.business.core.BaseReceiver;
import mspbots.cw.common.ResultMsg;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.Duration;

/**
 * @author AlexBob
 */
@Log4j2
@Component
@RequiredArgsConstructor
public class SlasReceiver extends BaseReceiver {

    private final SlasRepository slasRepository;

//    @RabbitListener(queues = "connect-wise.slas.tl")
    public void process(ResultMsg resultMsg) {
        JsonNode entity = resultMsg.getEntity();
        Slas data = super.objectMapper.convertValue(entity, Slas.class);
        data.setId(null);
        data.setSlasId(resultMsg.getId());
        data.setTenantCode(resultMsg.getTenantId());
        data.setSyncTime(resultMsg.getSyncDate());

        data.setLastUpdated(super.getDateTimeValue(entity.findValue("lastUpdated")));
        data.setSlasName(super.getStringValue(entity.get("name")));

        data.setHiImpactHiUrgencyId(super.getLongValueId(entity.get("hiImpactHiUrgency")));
        data.setHiImpactHiUrgencyName(super.getStringValueName(entity.get("hiImpactHiUrgency")));
        data.setHiImpactLowUrgencyId(super.getLongValueId(entity.get("hiImpactLowUrgency")));
        data.setHiImpactLowUrgencyName(super.getStringValueName(entity.get("hiImpactLowUrgency")));
        data.setHiImpactMedUrgencyId(super.getLongValueId(entity.get("hiImpactMedUrgency")));
        data.setHiImpactMedUrgencyName(super.getStringValueName(entity.get("hiImpactMedUrgency")));

        data.setLowImpactHiUrgencyId(super.getLongValueId(entity.get("lowImpactHiUrgency")));
        data.setLowImpactHiUrgencyName(super.getStringValueName(entity.get("lowImpactHiUrgency")));
        data.setLowImpactLowUrgencyId(super.getLongValueId(entity.get("lowImpactLowUrgency")));
        data.setLowImpactLowUrgencyName(super.getStringValueName(entity.get("lowImpactLowUrgency")));
        data.setLowImpactMedUrgencyId(super.getLongValueId(entity.get("lowImpactMedUrgency")));
        data.setLowImpactMedUrgencyName(super.getStringValueName(entity.get("lowImpactMedUrgency")));

        data.setMedImpactHiUrgencyId(super.getLongValueId(entity.get("medImpactHiUrgency")));
        data.setMedImpactHiUrgencyName(super.getStringValueName(entity.get("medImpactHiUrgency")));
        data.setMedImpactLowUrgencyId(super.getLongValueId(entity.get("medImpactLowUrgency")));
        data.setMedImpactLowUrgencyName(super.getStringValueName(entity.get("medImpactLowUrgency")));
        data.setMedImpactMedUrgencyId(super.getLongValueId(entity.get("medImpactMedUrgency")));
        data.setMedImpactMedUrgencyName(super.getStringValueName(entity.get("medImpactMedUrgency")));

        this.slasRepository.findBySlasIdAndTenantCode(resultMsg.getId(), resultMsg.getTenantId())
                .map(result -> {
                    data.setId(result.getId());
                    return data;
                })
                .defaultIfEmpty(data)
                .flatMap(this.slasRepository::save).block(Duration.ofSeconds(10));
    }
}
